package com.yanqu.road.server.logic.activity.common;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.activity.common.condition.BaseCommonActivityCondition;
import com.yanqu.road.server.manger.activity.ActivityMgr;

import java.util.ArrayList;
import java.util.List;

public class CommonActivityData {

    private GamePlayer player;

    private ActivityInfo activityInfo;

    private List<UserActivityConditionData> userActivityConditionDataList;

    private List<BaseCommonActivityCondition> baseCommonActivityConditionList;

    public CommonActivityData(GamePlayer player, ActivityInfo activityInfo, List<UserActivityConditionData> userActivityConditionDataList) {
        this.player = player;
        this.activityInfo = activityInfo;
        this.userActivityConditionDataList = userActivityConditionDataList;
        baseCommonActivityConditionList = new ArrayList<>();
    }

    public List<UserActivityConditionData> getUserActivityConditionDataList() {
        return userActivityConditionDataList;
    }

    public boolean isInTime() {
        return ActivityMgr.activityInTime(activityInfo);
    }

    public void addUserCommonActivityConditionData(UserActivityConditionData userConditionData) {
        userActivityConditionDataList.add(userConditionData);
    }

    public void addCondition(BaseCommonActivityCondition condition) {
        if (baseCommonActivityConditionList == null) {
            baseCommonActivityConditionList = new ArrayList<>();
        }
        baseCommonActivityConditionList.add(condition);
    }

    public void addToPlayer() {
        for (BaseCommonActivityCondition commonActivityCondition : baseCommonActivityConditionList) {
            commonActivityCondition.addTrigger(player);
        }
    }

    public void finish(int conditionId) {
        for (BaseCommonActivityCondition commonActivityCondition : baseCommonActivityConditionList) {
            if (commonActivityCondition.getConditionId() == conditionId) {
                commonActivityCondition.removeTrigger(player);
            }
        }
    }

    public int getActivityId() {
        return activityInfo.getActivityId();
    }

    public GamePlayer getPlayer() {
        return player;
    }

    public boolean continueAdd(int type) {
        for (BaseCommonActivityCondition condition : baseCommonActivityConditionList) {
            int conditionType = condition.getConditionType();
            if (conditionType == type) {
                return false;
            }
        }
        return true;
    }

    public UserActivityConditionData getUserActivityConditionData(int conditionId) {
        for (UserActivityConditionData conditionData : userActivityConditionDataList) {
            if (conditionData.getConditionId() == conditionId) {
                return conditionData;
            }
        }
        return null;
    }

    public BaseCommonActivityCondition getActivityCondition(int conditionId) {
        for (BaseCommonActivityCondition activityCondition : baseCommonActivityConditionList) {
            if (activityCondition.getConditionId() == conditionId) {
                return activityCondition;
            }
        }
        return null;
    }

    public UserActivityConditionData getFirstConditionData() {
        return userActivityConditionDataList.get(0);
    }

    public List<BaseCommonActivityCondition> getBaseCommonActivityConditionList() {
        return baseCommonActivityConditionList;
    }
}
